let root = [1, 2, 2, 3, 4, 4, 3]
function TreeNode(val, left, right) {
    this.val = (val === undefined ? 0 : val);
    this.left = (left === undefined ? null : left);
    this.right = (right === undefined ? null : right);
}
function arrayToTree(arr) {
    if (arr.length === 0 || arr[0] === null) return null;

    const root = new TreeNode(arr[0]);
    const queue = [root];
    let i = 1;

    while (queue.length > 0 && i < arr.length) {
        const current = queue.shift();

        // 处理左子节点
        if (i < arr.length && arr[i] !== null) {
            current.left = new TreeNode(arr[i]);
            queue.push(current.left);
        }
        i++;

        // 处理右子节点
        if (i < arr.length && arr[i] !== null) {
            current.right = new TreeNode(arr[i]);
            queue.push(current.right);
        }
        i++;
    }
    return root;
}
// var isSymmetric = function (root) {
//     if (!root) {
//         return true
//     }
//     let queue = [root]
//     while (queue.length > 0) {
//         const len = queue.length
//         const arr = []
//         let arr1 = []
//         for (let i = 0; i < len; i++) {
//             const current = queue.shift()
//             if (current.left) {
//                 queue.push(current.left)
//                 arr.push(current.left.val)
//                 arr1.push(current.left.val)
//             } else {
//                 arr.push('null')
//                 arr1.push('null')
//             }
//             if (current.right) {
//                 queue.push(current.right)
//                 arr.push(current.right.val)
//                 arr1.push(current.right.val)
//             } else {
//                 arr.push('null')
//                 arr1.push('null')
//             }
//         }
//         arr1.reverse()
//         for (let i = 0; i < arr.length; i++) {
//             if (arr1[i] !== arr[i]) {
//                 return false
//             }
//         }
//     }
//     return true
// };








var isSymmetric = function (root) {
    const queue = [root.left, root.right]
    while (queue.length > 0) {
        cur1 = queue.shift()
        cur2 = queue.shift()
        if (!cur1 && !cur2) {
            continue
        }
        if (!cur1 || !cur2 || cur1.val !== cur2.val) {
            return false
        }
        queue.push(cur1.left, cur2.right)
        queue.push(cur2.left, cur1.right)
    }
    return true

}
console.log(isSymmetric(arrayToTree(root)));


// var isSymmetric = function (root) {
//     if (!root) {
//         return true
//     }
//     let queue = [root.left, root.right]
//     while (queue.length > 0) {
//         const left = queue.shift()
//         const right = queue.shift()
//         if (!left && !right) {
//             continue
//         }
//         if (!left || !right || left.val !== right.val) {
//             return false
//         }
//         queue.push(left.left, right.right)
//         queue.push(left.right, right.left)
//     }
//     return true
// };





// function TreeNode(val, left, right) {
//     this.val = (val === null ? 0 : val),
//     this.left = (left === null ? 0 : left)
//     this.right = (right === null ? 0 : right)
// }
// function arrayToTree1(arr){
//     const root= new TreeNode(arr[0])
//     const queue=[root]
//     let i=1
//     while(queue.length>0){
//         const cur=queue.shift()
//         if(i<arr.length&&a[i]!==null){
//             cur.left=new TreeNode(arr[i])
//             queue.push(cur.left)
//         }
//         i++
//         if (i < arr.length && a[i] !== null) {
//             cur.right = new TreeNode(arr[i])
//             queue.push(cur.right)
//         }
//         i++
//     }
// }

